#region File Description
//-----------------------------------------------------------------------------
// MainMenuScreen.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.GamerServices;

using ScytheEngine;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
#endregion

namespace VTESGameLogicWindows
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    public class MainMenuScreen : MenuScreen
    {
        #region Initialization


        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base(Resources.MainMenu)
        {
            // Create our menu entries.
            MenuEntry singlePlayerMenuEntry = new MenuEntry(Resources.SinglePlayer);
            MenuEntry liveMenuEntry = new MenuEntry(Resources.PlayerMatch);
            MenuEntry systemLinkMenuEntry = new MenuEntry(Resources.SystemLink);
            MenuEntry exitMenuEntry = new MenuEntry(Resources.Exit);

			singlePlayerMenuEntry.Down = liveMenuEntry;

			liveMenuEntry.Up = singlePlayerMenuEntry;
			liveMenuEntry.Down = systemLinkMenuEntry;

			systemLinkMenuEntry.Up = liveMenuEntry;
			systemLinkMenuEntry.Down = exitMenuEntry;

			exitMenuEntry.Up = systemLinkMenuEntry;

            // Hook up menu event handlers.
            singlePlayerMenuEntry.Selected += SinglePlayerMenuEntrySelected;
            liveMenuEntry.Selected += LiveMenuEntrySelected;
            systemLinkMenuEntry.Selected += SystemLinkMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;

            // Add entries to the menu.
            AddMenuEntry( singlePlayerMenuEntry );
			AddMenuEntry( liveMenuEntry );
			AddMenuEntry( systemLinkMenuEntry );
			AddMenuEntry( exitMenuEntry );

			// set selected
			SelectedMenuEntry = singlePlayerMenuEntry;

			// set the position of the topmost menu entry
			MenuEntriesPosition = new Vector2(100, 250);
        }


        #endregion

        #region Handle Input


        /// <summary>
        /// Event handler for when the Single Player menu entry is selected.
        /// </summary>
        void SinglePlayerMenuEntrySelected(object sender, EventArgs e)
        {
			// First, we need to make sure a suitable gamer profile is signed in.
			ProfileSignInScreen profileSignIn = new ProfileSignInScreen( NetworkSessionType.PlayerMatch );

			// Hook up an event so once the ProfileSignInScreen is happy,
			// it will activate the CreateOrFindSessionScreen.
			profileSignIn.ProfileSignedIn += delegate
			{
			// TEST CODE: Add the signed in player and hard code 3 AI players for now
				List<IPlayer> list_of_players = new List<IPlayer>( 4 );
				list_of_players.Add( new Methuselah( Gamer.SignedInGamers[0] ) );
				list_of_players.Add( new Methuselah( "Player2" ) );
				list_of_players.Add( new Methuselah( "Player3" ) );
				list_of_players.Add( new Methuselah( "Player4" ) );


				GameFlowManager.Instance.InitializeGameFlowManager( list_of_players );

				LoadingScreen.Load( GameStateManager, true, new SinglePlayerGame() );
			};

			// Activate the ProfileSignInScreen.
			GameStateManager.AddState( profileSignIn );
        }


        /// <summary>
        /// Event handler for when the Live menu entry is selected.
        /// </summary>
        void LiveMenuEntrySelected(object sender, EventArgs e)
        {
            CreateOrFindSession(NetworkSessionType.PlayerMatch);
        }


        /// <summary>
        /// Event handler for when the System Link menu entry is selected.
        /// </summary>
        void SystemLinkMenuEntrySelected(object sender, EventArgs e)
        {
            CreateOrFindSession(NetworkSessionType.SystemLink);
        }


        /// <summary>
        /// Helper method shared by the Live and System Link menu event handlers.
        /// </summary>
        void CreateOrFindSession(NetworkSessionType sessionType)
        {
            // First, we need to make sure a suitable gamer profile is signed in.
            ProfileSignInScreen profileSignIn = new ProfileSignInScreen(sessionType);

            // Hook up an event so once the ProfileSignInScreen is happy,
            // it will activate the CreateOrFindSessionScreen.
            profileSignIn.ProfileSignedIn += delegate
            {
                GameStateManager.AddState(new CreateOrFindSessionScreen(sessionType));
            };

            // Activate the ProfileSignInScreen.
            GameStateManager.AddState(profileSignIn);
        }


        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel()
        {
            MessageBoxScreen confirmExitMessageBox =
                                    new MessageBoxScreen(Resources.ConfirmExitSample);

            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            GameStateManager.AddState(confirmExitMessageBox);
        }


        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, EventArgs e)
        {
            GameStateManager.Game.Exit();
        }


        #endregion
    }
}
